---
title: "Link Countries to UN Regions"
author: "Gina Reynolds and Matt Winters"
output: pdf_document
---

This file is used in replicating findings for "Foreign Aid Funnel? Placebo-Based Assessment of Aid Flows to  Non-Permanent U.N. Security Council Members" by Evangeline Reynolds (University of Illinois) and Matthew Winters (University of Illinois). 
 
```{r load packages}
library(knitr)
#opts_chunk$set(results="markup")
library(stringr)
```

Arab League
====

The list of countries that are part of the Arab League is documented at http://www.icnl.org/research/monitor/las.html which was accessed May 18th, 2015.

```{r read Arab League list}
#Note: Syria was suspended in 2011
League=read.csv("./01RawData/UNRegions - League of Arab States.txt")
```

Assign a Standard Name to the countries on the Arab League List
```{r remove Palestine - not a member of the UN}
#change working directory for this code chunck only to 000MyCountryCoder Folder
setwd("./000MyCountryCoder/") 
#load country namer function
source("countrycode2.R")
League$StandardName=FavoriteCountryName(League$LeagueArabStates)
```

Drop Palestine -  as not a member state of UN
```{r}
nrow(League); League=League[League$LeagueArabStates!="Palestine",]
#Verify that one row is dropped
nrow(League)
```

U.N. Regions Prior to 1966
=====

The data regarding the regional membership of countries prior to Security Council expansion are documented in *The Procedure of the UN Security Council* by Loraine Slevers and Sam Daws.  The screen shot provided in the raw data replication files was taken from https://books.google.com on May 6, 2015.  The .csv file is based on this information. 

For the 1946-1965 regions, we only assign a region if the country actually served on the Security Council.  

*Note:* There were more irregularities during this time period, especially in the Eastern European and Asian groups.  For example, some countries served on the Security Council for just one year; other countries took seats for regions of which they were not actually members.  Liberia took the Western European Seat in 1961.  Morroco, despite not being a member of the regional group, took the Middle Eastern seat in 1963-1964. 

```{r Read in data on Previous time period}
#Read in the data for the 1946-1965 Regional groups
EarlyDATA=read.csv("./01RawData/UNRegions - PreviousCountryRegionalGroups.csv",
                   na.strings ="")

#Turkey appears as part of two regions in the data
EarlyDATA[complete.cases(EarlyDATA),]

levels(EarlyDATA$Previous.Region1)
levels(EarlyDATA$Previous.Region2)
dim(EarlyDATA)[1]==length(unique(EarlyDATA$Country.Name))
```

**Standardize early regions country names data with the coder.**

```{r Create Standard Names}
#clean up leading and trailing spaces
EarlyDATA$Country.Name=str_trim(EarlyDATA$Country.Name)
#Assign Standard Name
EarlyDATA$StandardName=FavoriteCountryName(EarlyDATA$Country.Name)
#How many countries appear?
length(unique(EarlyDATA$StandardName))
```

**Transform early regions to country-year format**
```{r Transform Early}
options(width=100)
Year=1946:1965
StandardName=unique(EarlyDATA$StandardName)
EarlyCountryYear=merge(Year, StandardName)
names(EarlyCountryYear)=c("Year", "StandardName")

EarlyCountryYearRegions=merge(EarlyCountryYear, EarlyDATA)
dim(EarlyCountryYearRegions)
head(EarlyCountryYearRegions);tail(EarlyCountryYearRegions)
```


U.N. Regions from 1966 to the Present
===

We get modern information about the regional groupings from the U.N. website, ``United Nations Regional Groups of Member States" http://www.un.org/depts/DGACM/RegionalGroups.shtml, accessed May 27, 2015.

Several special cases are described on this website:  


*Israel*
*In May 2000, Israel became a Western Europe and Other States Group (WEOG) full member, on a temporary basis (subject to renewal), in WEOG's headquarters in the US, thereby enabling it to put forward candidates for election to various U.N. General Assembly bodies. In 2004 Israel obtained a permanent renewal for its membership.*

*Kiribati*
*As of 2010, Kiribati (geographically in Oceania) is not a member of any regional group, despite other Oceania nations belonging to the Asian group. Despite its membership in the United Nations, Kiribati has never delegated a permanent representative to the UN.*

*Turkey*
*Turkey, participates fully in both the Western Europe and Other States Group (WEOG) and the Asian Group, but for electoral purposes is considered a member of WEOG only.*

*United States of America*
*The United States of America is not a member of any regional group, but attends meetings of the Western Europe and Other States Group (WEOG) as an observer and is considered to be a member of that group for electoral purposes.*



Also, following the historical record, according to which region they have served for on the Security Council, I add Czechoslovakia, East Germany, and Yugoslavia to the list of Eastern European countries and West Germany to the Western European and Others Group (WEOG). 

```{r Add some countries not previously members}
ModernDATA=read.csv("./01RawData/UNRegions - UNModernCountryRegions.txt")
head(ModernDATA)
ModernDATA$Country.Name=str_trim(ModernDATA$Country.Name)
ModernDATA$Modern.Region=factor(str_trim(ModernDATA$Modern.Region))
head(ModernDATA)
tail(ModernDATA)

ModernDATA=rbind(ModernDATA, c("Yugoslavia","Eastern European Group"))
ModernDATA=rbind(ModernDATA, c("Czechoslovakia","Eastern European Group"))
ModernDATA=rbind(ModernDATA, c("East Germany","Eastern European Group"))
ModernDATA=rbind(ModernDATA, c("West Germany","Western European and Others Group (WEOG)"))

tail(ModernDATA)

```

Create a variable with the standardized country names:
```{r}
options(width=80)
ModernDATA$StandardName=FavoriteCountryName(ModernDATA$Country.Name)
dim(ModernDATA)[1]==length(unique(ModernDATA$StandardName))
```


Simplify/Shorten Region Names
------


```{r}
levels(ModernDATA$Modern.Region)
ModernDATA$Modern.Region=as.character(ModernDATA$Modern.Region)

ModernDATA$Modern.Region=str_replace(ModernDATA$Modern.Region, 
                                     "Latin American and Caribbean Group (GRULAC)",  
                                     "GRULAC")
ModernDATA$Modern.Region=str_replace(ModernDATA$Modern.Region, 
                                     "Western European and Others Group (WEOG)", 
                                     "WEOG")
unique(ModernDATA$Modern.Region)
```

**Add Arab League Indicator**
```{r}
ModernRegions=merge(ModernDATA, League, by="StandardName", all.x=TRUE)
head(ModernRegions)

ModernRegions$Modern.Region=paste0(ModernRegions$Modern.Region, 
                                   ifelse(is.na(ModernRegions$LeagueArabStates),"",
                                          " - League Arab States" ))

sort(unique(ModernRegions$Modern.Region))
```

```{r}
#write.csv(ModernRegions, "03_UNRegions - ModernRegions.csv")
#save(ModernRegions, file="03_UNRegions - ModernRegions.RData")
```


**Transform modern regions data to country-year format**
```{r}
options(width=80)
CurrentYear=as.numeric(substr(Sys.Date(),1,4))
Year=1966:CurrentYear
StandardName=unique(ModernRegions$StandardName)
ModernCountryYear=merge(Year, StandardName)
names(ModernCountryYear)=c("Year", "StandardName")
head(ModernCountryYear)
names(ModernCountryYear); names(ModernRegions)
ModernCountryYearRegions=merge(ModernCountryYear, 
                     ModernRegions)
dim(ModernCountryYearRegions)
```


Merge Early Regions with Modern Regions
=====

```{r}
options(width=80)
names(EarlyCountryYearRegions)
names(EarlyCountryYearRegions)=c( "StandardName", "Year", "Country.Name", "Region1", "Region2")
head(EarlyCountryYearRegions)
names(ModernCountryYearRegions)
ModernCountryYearRegions$LeagueArabStates=NA
names(ModernCountryYearRegions)=c( "StandardName", "Year", "Country.Name", "Region1", "Region2")
table(ModernCountryYearRegions$Region1)


head(ModernCountryYearRegions)

DATAAllRegionsLong=rbind(
                     EarlyCountryYearRegions[, which(names(EarlyCountryYearRegions) %in% 
                                             c("StandardName", "Year", "Region1", "Region2"))],
                     ModernCountryYearRegions[, which(names(ModernCountryYearRegions) %in% 
                                              c("StandardName", "Year",  "Region1", "Region2"))] 
                     )
dim(DATAAllRegionsLong)
head(DATAAllRegionsLong);tail(DATAAllRegionsLong)
table(DATAAllRegionsLong$Region1,useNA = "ifany")
table(DATAAllRegionsLong$Region2, useNA = "ifany")
```

Save Output
=====

```{r}
write.csv(DATAAllRegionsLong, "03_UNRegions - DATAAllRegionsLong.csv")
save(DATAAllRegionsLong, file="03_UNRegions - DATAAllRegionsLong.RData")
```

